package com.tensquare.search.controller;

import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by WF on 2020/12/20 15:37
 */
@RestController
@RequestMapping("article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    //1.根据文章标题和内容搜索
    @GetMapping("/search/{keywords}/{page}/{size}")
    public Result search(@PathVariable String keywords, @PathVariable Integer page, @PathVariable Integer size){
        System.out.println("keywords = " + keywords);
        //1.1)开始搜索
        PageResult<Article> articlePageResult = articleService.search(keywords,page,size);
        //1.2)返回
        return new Result(true, StatusCode.OK,"根据关键字分页查询成功！",articlePageResult);
    }

    //2.添加索引库
    @PostMapping
    public Result add(@RequestBody Article article){
        articleService.add(article);
        return new Result(true,StatusCode.OK,"添加到索引库成功！");
    }

    //3.查询所有
    @GetMapping
    public Result list(){
        Iterable<Article> articles = articleService.findAll();
        return new Result(true,StatusCode.OK,"查询成功",articles);
    }
}
